package com.wuyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyb.enums.ErrorCode;
import com.wuyb.mapper.ChatFilterMapper;
import com.wuyb.pojo.ChatFilter;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.entity.ResultUtils;
import com.wuyb.service.ChatFilterService;
import com.wuyb.util.MyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 聊天敏感词过滤服务实现类
 *
 * @Author: wyb
 * @Date: 2024/01/02/13:53
 * @Description:
 */
@Service
@Slf4j
public class ChatFilterServiceImpl extends ServiceImpl<ChatFilterMapper, ChatFilter> implements ChatFilterService {

    @Resource
    private ChatFilterMapper chatFilterMapper;

    @Override
    public BaseResponse<Page<ChatFilter>> select(Integer current, Integer size) {
        return ResultUtils.success((Page<ChatFilter>) chatFilterMapper.selectByUser(new Page<>(current,size)));
    }

    @Override
    public BaseResponse<?> updateOne(ChatFilter chatFilter) {
        int result = chatFilterMapper.updateById(chatFilter);
        if (result > 0) {
            return ResultUtils.success(chatFilter, "修改成功");
        }

        return ResultUtils.error(ErrorCode.OPERATION_ERROR);
    }

    @Override
    public BaseResponse<?> insert(ChatFilter chatFilter) {
        chatFilter.setAppenderId(MyUtil.getUser().getId());
        chatFilter.setAppendTime(LocalDateTime.now());
        int result = chatFilterMapper.insert(chatFilter);
        if (result > 0) {
            return ResultUtils.success("添加成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加失败");
    }

    @Override
    public BaseResponse<?> delete(Integer id) {
        int delete = chatFilterMapper.deleteById(id);
        if (delete > 0) {
            return ResultUtils.success("删除成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR, "删除失败");
    }

    @Override
    public BaseResponse<?> setEnable(Integer id, boolean b) {
        int result = chatFilterMapper.updateById(new ChatFilter().setId(id).setEnable(b));
        if (result > 0) {
            return ResultUtils.success("修改成功");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR,"修改失败");
    }

    @Override
    public String filter(String msg) {
        List<ChatFilter> filterItems = chatFilterMapper.selectList(new QueryWrapper<ChatFilter>().eq("enable_xq", 1));
        for (ChatFilter filterItem : filterItems) {
            if (filterItem.getFilterRule() == 1) {
                msg = msg.replace(filterItem.getFilterContent(), filterItem.getReplaceContent());
            } else {
                msg = msg.replaceAll(filterItem.getFilterContent(), filterItem.getReplaceContent());
            }
        }
        return msg;
    }
}
